package cn.hx.mallorder.controller;

import cn.hx.mallorder.entity.OmsOrder;
import cn.hx.mallorder.service.OmsOrderItemService;
import cn.hx.mallorder.service.OmsOrderService;
import cn.hx.mallorder.util.JwtUtils;
import cn.hx.malluser.vo.UmsMemberVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class PublicController {
    @Resource
    private OmsOrderService orderService;
    @Autowired
    private OmsOrderItemService omsOrderItemService;
    @Autowired
    private ProductClient productClient;

    @Autowired
    RedisTemplate redisTemplate;

    public UmsMemberVo getUserInfo(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Long id=0L;
        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())){
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)){
                        Claims claims = JwtUtils.getClaims(value);
                        id =  Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }

        UmsMemberVo userInfo = null;
        try{
            userInfo = (UmsMemberVo) redisTemplate.opsForValue().get("user_" + id); //空指问题
        }catch (Exception e){
        }
        return userInfo;
    }

    @GetMapping({"all-order","/"})
    public String index() {
        return "all-order";
    }

    /**
     *前端页面测试
     */
    @GetMapping("{path}")
    public String togo(@PathVariable("path") String path) {
        return path;
    }

    @GetMapping("/allOrderList")
    @ResponseBody
    public  List<Object> test(){
        Long id2= orderService.findId(2L);
        Integer id1= omsOrderItemService.findItemById(id2);
        List<Object> list = productClient.getList(id1);
        return list;
    }
}
